package com.platform.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.platform.dao.CategoryDao;
import com.platform.dao.CollectDao;
import com.platform.entity.CategoryEntity;
import com.platform.entity.CollectEntity;
import com.platform.entity.SysUserEntity;

/**
 * Service接口
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-21 15:32:31
 */
@Service("categoryService")
public class CategoryService {
    @Autowired
    private CategoryDao categoryDao;
	@Autowired
	private CollectDao collectDao;

    public CategoryEntity queryObject(Integer id) {
        return categoryDao.queryObject(id);
    }

    public CategoryEntity queryObject(Integer id,Integer userId) {
    	CategoryEntity category = categoryDao.queryObject(id);
    	if(userId != null) {
    		CollectEntity collect = collectDao.existsCollect(userId, id,1);
    		category.setCollect(collect==null?0:1);
    	}
        return category;
    }

    public List<CategoryEntity> queryList(Map<String, Object> map) {
        return categoryDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return categoryDao.queryTotal(map);
    }

    public int save(CategoryEntity category) {
        if ("L1".equals(category.getLevel())) {
            category.setParentId(0);
        }
        return categoryDao.save(category);
    }

    public int update(CategoryEntity category) {
        if ("L1".equals(category.getLevel())) {
            category.setParentId(0);
        }
        return categoryDao.update(category);
    }

    public int delete(Integer id) {
        return categoryDao.delete(id);
    }

    @Transactional
    public int deleteBatch(Integer[] ids) {
        categoryDao.deleteByParentBatch(ids);
        return categoryDao.deleteBatch(ids);
    }
}
